package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.entity.Hospital;
import com.example.demo.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("hospital")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;

    @GetMapping("/hospital/{id}")
    @ResponseBody
    public Hospital getHospitalById(@PathVariable Long id) {
        return hospitalService.getById(id);
    }

    @GetMapping("/list")
    public String getHospital(Model model,Hospital hospital) {
        List<Hospital> list = null;
        if(hospital.getHospitalName() != null && !"".equals(hospital.getHospitalName())) {
            list = hospitalService.lambdaQuery()
                    .like(Hospital::getHospitalName,hospital.getHospitalName())
                    .list();
            model.addAttribute("hospitalName", hospital.getHospitalName());
        }else{
            list = hospitalService.list();
        }
        model.addAttribute("list", list);
        return "hospital/list";
    }

    @GetMapping("addpage")
    public String addpage(Model model, @ModelAttribute Hospital hospital, @RequestParam(value = "id",required = false) Integer id){
        if(!StringUtils.isEmpty(id)){
            Hospital byId = hospitalService.getById(id);
            model.addAttribute("hospital",byId);
        }
        return "hospital/add";
    }

    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    public String saveOrUpdate(Model model, Hospital hospital, Principal principal) {
        String loginName = principal.getName();
        Date now = new Date();
        hospital.setCreatePerson(loginName);
        hospital.setUpdatePerson(loginName);
        if(hospital.getId() == null){
            hospital.setCreated(now);
        }
        hospital.setUpdated(now);
        hospitalService.saveOrUpdate(hospital);
        return getHospital(model,hospital);
    }


    @GetMapping("/page")
    public Page<Hospital> getAllHospital(Integer pageNo, Integer pageSize) {
        Page<Hospital> page = hospitalService.lambdaQuery()
                .eq(Hospital::getCapacity,"1000")
                .page(new Page<>(pageNo, pageSize));
        return page;
    }

    @PostMapping("/save")
    public String saveHospital(@RequestBody Hospital hospital) {
        Date now = new Date();
        hospital.setCreated(now);
        hospital.setUpdated(now);
        hospitalService.save(hospital);
        return "success";
    }

    @PutMapping("/update")
    public String updateHospital(@RequestBody Hospital hospital) {
        hospitalService.updateById(hospital);
        return "success";
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public String deleteHospitalById( @PathVariable Long id) {
        hospitalService.removeById(id);
        return "success";
    }
}
